home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / FileBrowser / Inspector.subproj / FileInspector.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  4.4 KB  |  170 lines

  1. //        Written by Todd Thomas Copyright (c) 1995 by Todd Thomas.
  2. //                Version 1.0.  All rights reserved.
  3. //
  4. //        This notice may not be removed from this source code.
  5. //
  6. //    This object is included in the MiscKit by permission from the author
  7. //    and its use is governed by the MiscKit license, found in the file
  8. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9. //    for a list of all applicable permissions and restrictions.
  10. //    
  11.  
  12. #import <appkit/appkit.h>
  13. #import <misckit/MiscTime.h>
  14. #import <misckit/MiscClockView.h>
  15. #import <misckit/MiscClockView_MiscTime.h>
  16. #import <misckit/MiscFile.h>
  17. #import <misckit/MiscUser.h>
  18. #import <misckit/MiscUserGroup.h>
  19. #import "FileInspector.h"
  20.  
  21.  
  22. @implementation FileInspector
  23.  
  24. - selectedFile: (MiscFile *)theFile
  25. {
  26.     // There is a selected file to inspect. Get all of it's relevant
  27.     // information and display in the inspector.
  28.     char  tmp[20];
  29.     int  fsize;
  30.     MiscTime  *changeTime;
  31.     MiscFile  *linkSource;
  32.     MiscUser  *fileOwner;
  33.     MiscUserGroup  *fileGroup;
  34.     unsigned short  perms;
  35.     
  36.     if (theFile == nil)
  37.         return self;
  38.     
  39.     // If permissions are changed, then we need to know which file.
  40.     fileSelected = theFile;
  41.  
  42.     // Turn off stat caching so we get the most up to date information.
  43.     [theFile setEnableCaching: NO];    
  44.     fsize = [theFile size];
  45.     // Turn on caching so all the calls below don't stat the file again.
  46.     [theFile setEnableCaching: YES];
  47.         
  48.     [filename setStringValue: [theFile filename] ];
  49.     [path setStringValue: [theFile fullPath] ];
  50.     if ([theFile isSymbolicLink])
  51.     {
  52.         [linktoTitle setTextGray: NX_BLACK];
  53.         linkSource = [theFile symbolicLinkSource];
  54.         [linkto setStringValue: [linkSource fullPath] ];
  55.         linkSource = [linkSource free];
  56.      }
  57.     else
  58.     {
  59.         [linkto setStringValue: ""];
  60.         [linktoTitle setTextGray: NX_DKGRAY];
  61.      } 
  62.      
  63.     if (fsize < 1024*100)
  64.         sprintf (tmp, "%d bytes", fsize);
  65.     else
  66.         sprintf (tmp, "%2.3f MB", (float)fsize/(1024*1024));
  67.     [size setStringValue: tmp];
  68.     
  69.     [imageView setImage: [theFile iconImage] ];
  70.     changeTime = [theFile lastModifyTime];
  71.     [clock setMiscTime: changeTime];
  72.     [changeTime free];
  73.     
  74.     perms = [theFile permissions];
  75.     [ [permissionMatrix cellAt: 2 : 2] setState: (perms & 0x0001)];
  76.     [ [permissionMatrix cellAt: 1 : 2] setState: (perms & 0x0002)];
  77.     [ [permissionMatrix cellAt: 0 : 2] setState: (perms & 0x0004)];
  78.  
  79.     [ [permissionMatrix cellAt: 2 : 1] setState: (perms & 0x0008)];
  80.     [ [permissionMatrix cellAt: 1 : 1] setState: (perms & 0x0010)];
  81.     [ [permissionMatrix cellAt: 0 : 1] setState: (perms & 0x0020)];
  82.  
  83.     [ [permissionMatrix cellAt: 2 : 0] setState: (perms & 0x0040)];
  84.     [ [permissionMatrix cellAt: 1 : 0] setState: (perms & 0x0080)];
  85.     [ [permissionMatrix cellAt: 0 : 0] setState: (perms & 0x0100)];
  86.     [permissionMatrix display];
  87.     
  88.     fileOwner = [theFile owner];
  89.     [owner setStringValue: [fileOwner username] ];
  90.     [fileOwner free];
  91.     
  92.     fileGroup = [theFile group];
  93.     [group setStringValue: [fileGroup groupName] ];
  94.     [fileGroup free];
  95.     
  96.     return self;
  97. }
  98.  
  99.  
  100. - permissionsChanged: sender
  101. {
  102.     // Activated when one of the permissions radio buttons is changed.
  103.     // We should then try to change the permissions for the selected
  104.     // file.
  105.     int  selectedCol = [sender selectedCol];
  106.     int  selectedRow = [sender selectedRow];
  107.     int  state = [ [sender selectedCell] state];
  108.     int  permToChange = 0;
  109.     int  who = 0;
  110.     int  succ;
  111.     
  112.     // If somehow this gets called with no file selected, quit.
  113.     if (fileSelected == nil)    
  114.         return self;
  115.         
  116.     switch (selectedCol)
  117.     {
  118.         case 0:    who = MISCFILE_OWNER; break;
  119.         case 1: who = MISCFILE_GROUP; break;
  120.         case 2: who = MISCFILE_OTHER; break;
  121.      }
  122.     
  123.     switch (selectedRow)
  124.     {
  125.         case 0: permToChange = MISCFILE_READ; break;
  126.         case 1: permToChange = MISCFILE_WRITE; break;
  127.         case 2: permToChange = MISCFILE_EXECUTE; break;
  128.      }
  129.     
  130.     if (state == 0)
  131.         // Was 1, so removing a permission.
  132.         succ = [fileSelected removePermissions: permToChange for: who];
  133.     else
  134.         // Was 0, now 1, so adding a permission.
  135.         succ = [fileSelected addPermissions: permToChange for: who];
  136.         
  137.     if (succ == MISCFILE_ERROR)
  138.     {
  139.         // The change was not allowed, so update the matrix to reflect that.
  140.         [ [sender selectedCell] setState: !(state)];
  141.         [sender display];
  142.      }
  143.     
  144.     return self;
  145. }
  146.  
  147.     
  148. - window
  149. {
  150.     // The inspector window, so when loaded, we can tell it to show itself.
  151.     return inspectorWin;
  152. }
  153.  
  154. @end
  155.  
  156.  
  157. @implementation FileInspector (NibInitialization)
  158.  
  159. - awakeFromNib
  160. {
  161.     // Setup the MiscClippedTextFields.
  162.     [path setBordered: NO];
  163.     [path setBackgroundGray: NX_LTGRAY];
  164.     [linkto setBordered: NO];
  165.     [linkto setBackgroundGray: NX_LTGRAY];
  166.     return self;
  167. }
  168.  
  169. @end
  170.